/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
*/

package com.jinhe.tss.core.cachepool.extend.assignment;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

import com.jinhe.tss.core.cachepool.CacheManager;
import com.jinhe.tss.core.cachepool.Cacheable;
import com.jinhe.tss.core.cachepool.IPool;
import com.jinhe.tss.core.cachepool.strategy.CacheConstants;

/** 
 * <p> RecordsOutputTask.java </p> 
 * 
 * 输出记录到数据库的任务抽象超类。
 * 类似日志输出、访问量输出可以通过继承该超类实现。
 * 
 * @author Jon.King 2007-2-15
 */
public abstract class RecordsOutputTask implements IAssignment {
    
    protected Logger log = Logger.getLogger(this.getClass());
    
    private static int count = 0;
    private String name = "";
    
    public RecordsOutputTask(){
        count ++;
        String className = this.getClass().getName();
        name = className.substring(className.lastIndexOf(".") + 1) + "_" + count;
    }
    
    public String toString(){
        return name;
    }
    
    protected List<Object> records;

    public void fill(List<Object> records) {
        this.records = records;
    }

    public void recycle() throws Exception {
        this.records = null;
    }

    public void excute() {
        if(records == null) return;
        
        IPool connectionPool = CacheManager.getInstance().getCachePool(CacheConstants.CONNECTION_POOL);
        Cacheable connItem = connectionPool.checkOut(0);
        Connection conn = (Connection) connItem.getValue();
        try {
            createRecords(conn);
        } catch (SQLException e) {
            log.error("写入记录到数据库时候出错", e);
        }finally{
            connectionPool.checkIn(connItem);
        }
    }
    
    protected abstract void createRecords(Connection conn) throws SQLException;
    
}


